import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:smart_devices/model/spare_part.dart';
import 'package:smart_devices/model/upkeep_task.dart';

class UpkeepTaskLogic extends GetxController {
  RxString search = RxString('');

  late RefreshController refreshController;

  Rx<SparePartModel> details = Rx(
      SparePartModel(imgUrl: '', name: '', code: '', model: '', count: 0, change: 0, inventory: 0));

  RxList<UpkeepTaskModel> pageList = RxList([]);

  @override
  void onInit() {
    super.onInit();
    for (var i = 0; i < 10; i++) {
      pageList.add(UpkeepTaskModel(
          title: '电梯设备维修02',
          taskNo: 'BX-2023041648',
          name: '电梯设备',
          code: 'SB0792789',
          location: '生产车间二层',
          addTime: '2023.11.14 08:32:31'));
    }
    refreshController = RefreshController(initialRefresh: false);
    if (Get.arguments != null) {
      details.value = Get.arguments as SparePartModel;
    }
  }

  @override
  void onClose() {
    super.onClose();
    refreshController.dispose();
  }

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 15) {
      return refreshController.loadNoData();
    }

    /// 添加
    refreshController.loadComplete();
    pageList.add(UpkeepTaskModel(
        title: '电梯设备维修02',
        taskNo: 'BX-2023041648',
        name: '电梯设备',
        code: 'SB0792789',
        location: '生产车间二层',
        addTime: '2023.11.14 08:32:31'));
  }
}
